import '../dao/ImGroupDao.dart';
import '../dao/ImSessionDao.dart';
import '../pojo/ImPojo.dart';
import '../socket/dto/business/PullData.dart';
import '../util/ImCacheFactory.dart';
import 'BaseService.dart';

class ImSessionService extends BaseService {

  // final _sessionDao = ImSessionDao();
  // final _groupDao = ImGroupDao();

  Future<int> saveOrUpdates(String currentUserId, List<SessionItem> sessionItemList) async {
    if (sessionItemList.isEmpty) return 0;

    final sessionDao = ImSessionDao();
    final groupDao = ImGroupDao();

    var insertSessionSet = <ImSession>[];
    var updateSessionSet = <ImSession>[];
    for(var sessionItem in sessionItemList) {
      String? toUserId = sessionItem.userId;
      ImSession imSession = await _toImSession(currentUserId, sessionItem);
      String? groupCode = sessionItem.groupId;
      ImSession? imDbSession = null;
      if (groupCode != null) { // 群聊
        ImGroup? imGroup = await groupDao.getByCodeAndUserId(groupCode, currentUserId);
        imDbSession = await sessionDao.getByGroupCodeAndUserId(imGroup!.code, currentUserId);
      }
      if (toUserId != null) { // 单聊
        imDbSession = await sessionDao.getByUserId(currentUserId, currentUserId, toUserId);
      }
      if (imDbSession == null) {
        insertSessionSet.add(imSession);
      } else {
        imSession.id = imDbSession.id;
        imSession.latelyMessage = imDbSession.latelyMessage;
        imSession.hotMessageTotal = imDbSession.hotMessageTotal;
        updateSessionSet.add(imSession);
      }
    }

    await sessionDao.batchInserts(insertSessionSet);
    await sessionDao.batchUpdates(currentUserId, updateSessionSet);
    return sessionItemList.length;
  }

  Future<ImSession?> getById(int sessionId) async {
    final sessionDao = ImSessionDao();

    var session = await sessionDao.getById(sessionId);
    await initSessionInfo(session!);
    return session;
  }

  Future<void> initSessionInfo(ImSession session) async {
    final groupDao = ImGroupDao();

    var groupCode = session.groupCode;
    if (groupCode != null) { // 群聊
      ImGroup? imGroup = await groupDao.getByCodeAndUserId(groupCode, session.currentUserId!);
      session.group = imGroup;
      for (var gu in imGroup!.groupUserList!) {
        gu.user = await ImCacheFactory.getInstance().getImUser(gu.userId);
      }
    } else {
      var user = await ImCacheFactory.getInstance().getImUser(session.toUserId!);
      session.toUser = user;
    }
  }

  Future<ImSession> _toImSession(String currentUserId, SessionItem sessionItem) async {
    var imSession = null;
    var groupCode = sessionItem.groupId;
    if (groupCode != null) {
      var group = await ImCacheFactory.getInstance().getImGroup(currentUserId, groupCode);
      imSession = ImSession.buildWithGroup(currentUserId, group!);
    }

    var userId = sessionItem.userId;
    if (userId != null) {
      var user = await ImCacheFactory.getInstance().getImUser(userId);
      imSession = ImSession.buildWithUser(currentUserId, user!);
    }

    imSession.currentUserId = currentUserId;
    imSession.serverId = sessionItem.serverId;
    imSession.isTop = sessionItem.isTop;
    imSession.isDisturb = sessionItem.isDisturb;
    imSession.isTop2 = sessionItem.isTop2;
    imSession.isDisturb2 = sessionItem.isDisturb2;
    imSession.status = ImSessionStatus.ofValue(sessionItem.status.value);
    imSession.serverId = sessionItem.serverId;
    imSession.updateTime = sessionItem.updateTime;
    imSession.createTime = sessionItem.updateTime;

    return imSession;
  }
}